package com.jtmg.glorykingsnews.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jtmg.glorykingsnews.domain.TbManager;
import com.jtmg.glorykingsnews.domain.TbMarket;
import com.jtmg.glorykingsnews.domain.TbUser;
import com.jtmg.glorykingsnews.mapper.TbMarketMapper;
import com.jtmg.glorykingsnews.vo.commom.LayuiReturnVo;
import com.jtmg.glorykingsnews.vo.commom.MsgVo;
import com.jtmg.glorykingsnews.vo.manager.ManagerQuery;
import com.jtmg.glorykingsnews.vo.manager.ManagerVo;
import com.jtmg.glorykingsnews.vo.manager.ManagerVoForQuery;
import com.jtmg.glorykingsnews.vo.user.UserVoForQuery;
import org.apache.catalina.Manager;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jtmg.glorykingsnews.mapper.TbManagerMapper;
import com.jtmg.glorykingsnews.service.TbManagerService;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TbManagerServiceImpl implements TbManagerService{

    @Resource
    private TbManagerMapper tbManagerMapper;

    @Resource
    private TbMarketMapper tbMarketMapper;

    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String currentTime = formatter.format(date);

    @Override
    public MsgVo loginSuper(ManagerVo vo) {
        if("admin".equals(vo.getUsername()) && "admin".equals(vo.getPassword())){
            return new MsgVo("200","超级管理员登陆成功");
        }
        return new MsgVo("201","账号或密码不正确");
    }

    @Override
    public String loginManager(ManagerVo vo, HttpServletRequest request) {
        Example example = new Example(TbManager.class);
        example.createCriteria().andEqualTo("username", vo.getUsername());
        TbManager manager = tbManagerMapper.selectOneByExample(example);
        if(manager!=null && manager.getPassword().equals(DigestUtils.md5DigestAsHex(vo.getPassword().getBytes()))){
            if (manager.getDeleted() == 1){
                return "html/managerLogin";
            }
            //把管理员信息放到全局，供其他接口获取
            HttpSession session = request.getSession();
            session.setAttribute("managerInfo", manager);
            System.out.println("出来啊"+session.getAttribute("managerInfo"));
            return "http://localhost:63342/glorykingsnews/templates/html/myadmin";
        }
         return "html/managerLogin";
    }

    @Override
    public MsgVo<TbManager> registerManager(ManagerVo vo) {
        MsgVo<TbManager> msgVo = new MsgVo<>();
        System.out.println(vo);

        Example example = new Example(TbManager.class);
        example.createCriteria().andEqualTo("username", vo.getUsername());
        List<TbManager> nManagers = tbManagerMapper.selectByExample(example);

        Example example1 = new Example(TbManager.class);
        example1.createCriteria().andEqualTo("phone", vo.getPhone());
        List<TbManager> pManagers = tbManagerMapper.selectByExample(example1);
        //根据用户名查询是否有重名的用户
        if(nManagers.size() > 0){
            System.out.println(nManagers.get(0).getName());
            msgVo.setCode("201");
            msgVo.setMsg("该用户已被注册");
            //根据电话号码查询是否有重复的电话号码
        } else if(pManagers.size() > 0){
            msgVo.setCode("201");
            msgVo.setMsg("该号码已被注册");
        }else {
            //注册到数据库
            TbManager manager = new TbManager();
            manager.setId(UUID.randomUUID().toString());
            manager.setUsername(vo.getUsername());
            manager.setPassword(DigestUtils.md5DigestAsHex(vo.getPassword().getBytes()));
            manager.setName(vo.getName());
            manager.setPhone(vo.getPhone());
            manager.setMid(vo.getMid());
            manager.setCreateTime(new Date());
            manager.setModifyTime(new Date());
            manager.setDeleted(0);
            tbManagerMapper.insert(manager);
            msgVo.setCode("200");
            msgVo.setMsg("注册成功");
            System.out.println("333");
        }
        System.out.println(msgVo.getMsg());

        return msgVo;
    }

    @Override
    public MsgVo deleteManager(String id) {
        System.out.println(id);
        try{
            TbManager manager = tbManagerMapper.selectByPrimaryKey(id);
            if (manager == null){
                return new MsgVo("201","该管理员不存在");
            }
            manager.setDeleted(1);
            manager.setModifyTime(new Date());
            tbManagerMapper.updateByPrimaryKey(manager);
            return new MsgVo("200","删除成功");
        } catch (Exception e){
            return new MsgVo("201","删除异常");
        }
    }

    @Override
    public LayuiReturnVo<ManagerVoForQuery> queryManager(ManagerQuery query)  {
       Example example = new Example(TbManager.class);
       Example.Criteria criteria =example.createCriteria().andEqualTo("deleted",0);
       String name = query.getmName();
       if(name != null && !"".equals(name)){
           criteria.andLike("name","%"+name+"%");
       }
       String phone = query.getPhone();
        if(phone != null && !"".equals(phone)){
            criteria.andLike("phone","%"+phone+"%");
        }
//        PageHelper.startPage(query.getPage(),query.getLimit());
        List<TbManager> managerList = tbManagerMapper.selectByExample(example);
        int count = tbManagerMapper.selectCountByExample(example);
        List<ManagerVoForQuery> managerVoForQueries = new ArrayList<>();
        for (TbManager manager : managerList){
            ManagerVoForQuery managerVoForQuery = new ManagerVoForQuery();
            //根据管理员表的夜市id查询夜市名称
            int mid = manager.getMid();
            TbMarket market= tbMarketMapper.selectByPrimaryKey(mid);
            if (market != null){
                managerVoForQuery.setMarketName(market.getMarketName());
            }
            //封装反显信息
            managerVoForQuery.setId(manager.getId());
            managerVoForQuery.setUsername(manager.getUsername());
            managerVoForQuery.setPassword(manager.getPassword());
            managerVoForQuery.setSalt(manager.getSalt());
            managerVoForQuery.setName(manager.getName());
            managerVoForQuery.setPhone(manager.getPhone());
            managerVoForQuery.setMid(manager.getMid());
            managerVoForQuery.setCreateTime(formatter.format(manager.getCreateTime()));
            managerVoForQuery.setModifyTime(formatter.format(manager.getModifyTime()));
            managerVoForQuery.setDeleted(manager.getDeleted());
            //添加到反显的集合中
            managerVoForQueries.add(managerVoForQuery);
        }
        return new LayuiReturnVo<ManagerVoForQuery>(0,"查询用户信息成功",count,managerVoForQueries);
    }
}
